package com.chinasofti.lianxi;


public class SellTicket implements Runnable {
    private  int tickets = 100;
    private Object obj = new Object();
    @Override
    public void run() {
     while (true){
         synchronized (obj) {
             if (tickets <= 0) {
                 break;//卖完了
             } else {
                 try {
                     Thread.sleep(100);
                     //t1休息了100毫秒
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
                 //窗口一售票一张
                 tickets--;
                 System.out.println(Thread.currentThread().getName() + "在卖票 还剩下" + tickets + "张");
             }
         }//t1从这里出来了，这段代码的锁就释放了
     }
    }
}
class SellTicketDemo{
    public static void main(String[] args) {
       SellTicket st = new  SellTicket();
        Thread t1 = new Thread(st,"窗口1");
        Thread t2 = new Thread(st,"窗口2");
        Thread t3 = new Thread(st,"窗口3");
        t1.start();
        t2.start();
        t3.start();

    }
}